/*
 * SDLGameWindow.cpp
 *
 *  Created on: 20/05/2014
 *      Author: lucio
 */

#include "SDLGameWindow.h"
//The attributes of the screen
const int SCREEN_WIDTH = 1928;
const int SCREEN_HEIGHT = 962;
const int SCREEN_BPP = 32;

SDL_Surface *load_image( std::string filename ) {
	//Temporary storage for the image that's loaded
	SDL_Surface* loadedImage = NULL;
	//The optimized image that will be used
	SDL_Surface* optimizedImage = NULL;
	//Load the image
	loadedImage = IMG_Load( filename.c_str() );

	//If nothing went wrong in loading the image
	if( loadedImage != NULL ) {
		//Create an optimized image
		optimizedImage = SDL_DisplayFormat( loadedImage );
		//Free the old image
		SDL_FreeSurface( loadedImage );
	}
	//Return the optimized image
	return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination ) {
	//Make a temporary rectangle to hold the offsets
	SDL_Rect offset;
	//Give the offsets to the rectangle
	offset.x = x; offset.y = y;
	//Blit the surface
	SDL_BlitSurface( source, NULL, destination, &offset );
}


int SDL_GameWindow::execute(){
	//Initialize all SDL subsystems
	if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) { return 1; }

	this->screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

	if(this->screen == NULL ) {
		return 1;
	}

	//Load the images
	background = load_image( "/home/lucio/background.jpg" );

	//Apply the background to the screen
	apply_surface( 0, 0, this->background, this->screen );

	if( SDL_Flip( this->screen ) == -1 ) {
		return 1;
	}
	SDL_Delay( 10000 );

	SDL_FreeSurface( message );
	SDL_FreeSurface( background );
	//Quit SDL
	SDL_Quit();
	return 0;
}

SDL_GameWindow::SDL_GameWindow() {
	this->background = NULL;
	this->message = NULL;
	this->screen = NULL;
}

SDL_GameWindow::~SDL_GameWindow() {
	// TODO Auto-generated destructor stub
}

